package com.swanclouds.redfish.systems;

import com.google.common.base.Preconditions;
import com.swanclouds.api.systems.ProcessorService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.internal.BaseSystemService;
import com.swanclouds.redfish.systems.domain.ProcessorModel;

import java.util.ArrayList;
import java.util.List;

/**
 * CPU集合资源
 *  
 * @author 张立强
 */
public class ProcessorServiceImpl extends BaseSystemService implements ProcessorService {

    /**
     * 查询 CPU 集合资源信息
     * @return CollectionResource
     */
    @Override
    public CollectionResource get() {
        return get(CollectionResourceModel.class,uri(ClientConstants.SYSTEMS_PROCESSORS,getSystemId())).execute();
    }

    /**
     * 查询 CPU 资源实例
     * @param cpuId id
     * @return Processor
     */
    @Override
    public ProcessorModel getProcessor(String cpuId) {
        Preconditions.checkNotNull(cpuId, "CPU Id is null");
        return get(ProcessorModel.class,uri(ClientConstants.SYSTEMS_PROCESSORS_ID,getSystemId(),cpuId)).execute();
    }

    /**
     * 查询 CPU 资源实例列表
     * @return List&lt;Processor&gt;
     */
    @Override
    public List<ProcessorModel> list() {
        List<ProcessorModel> processors = new ArrayList<>();
        CollectionResource collectionResource = get();
        collectionResource.getCollectionResourceList().stream().forEach(ref -> {
            processors.add(get(ProcessorModel.class, uri(ref)).execute());
        });
        return processors;
    }
}
